<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/10 0010
 * Time: 下午 1:37
 */
namespace app\index\controller;

use think\Controller;
use think\Model;
use think\Session;
use think\Request;

class Common extends Controller {
    /**
     * @title   初始化网站数据
     */
    public function _initialize()
    {
        $allway = ["Index/index","Index/prddetail","Indent/indent","Indent/pay","Indent/payorder",'Login/index','Indent/done','Indent/isuser','Login/logout','Login/forgetpass','Login/phonecode'];
        $url = ucfirst(Request::instance()->controller()).'/'.Request::instance()->action();
        if(!in_array($url,$allway)){
            if(!Session::has('userid')) {
                if (Request::instance()->isAjax()) $this->error('登陆已过期，请重新登陆');
                $this->redirect(url('login/index'));
            }
        }
    }

    /**
     * @param Model $model              传递模型
     * @param $data                     传递数组
     * @param $type                     0：不使用回调 1：使用回调
     * @param \Closure|null $closure    使用回调灵活操作业务
     * @param $valname                  验证器的名称
     */
    public function store(Model $model, $data, $valname=null, $type=0, \Closure $closure=null)
    {
        $res = $model->store($data, $valname);
        if($type==1) {
            $closure($res);
        }else{
            return $res;
        }
    }
}